package com.fearless.crm.settings.service.impl;

import com.fearless.crm.exception.LoginException;
import com.fearless.crm.settings.dao.UserDao;
import com.fearless.crm.settings.entity.User;
import com.fearless.crm.settings.service.UserService;
import com.fearless.crm.utils.DateTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private  UserDao userDao;


//登录验证操作,
    public User loginService(String loginAct,String loginPwd,String ip)throws LoginException{
        Map<String,Object> map=new HashMap<>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        User user=userDao.loginDao(map);
        if(user==null){
            //程序走到意味着查询不到记录，就是账号密码错误
            throw new LoginException("账号或者密码错误");
        }

        //如果程序执行到这里，说明账号密码正确
        //需要继续向下验证其他三项信息

        //1.验证失效时间
        String expireTime=user.getExpireTime();
        String currentTime= DateTimeUtil.getSysTime();
        if(expireTime.compareTo(currentTime)<0){
            throw new LoginException("已经过了失效时间");
        }

        //2.验证锁定状态
        String lockState=user.getLockState();
        if("0".equals(lockState)){
            throw new LoginException("已经锁定");
        }

        //3.验证ip地址
        String allowIps=user.getAllowIps();

        if(allowIps !=null && allowIps !=""){
            if(!allowIps.contains(ip)){
                throw new LoginException("该ip地址无权访问");
            }
        }


        return user;

    }


//获取用户列表，用于铺垫所有者下拉框
    public List<User> getUserListService() {
        List<User> userList=userDao.getUserListDao();
        return userList;
    }

//

}
